package com.sline.file;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo5_FileMethod {
    public static void main(String[] args) throws IOException {
        File file = new File("abc");
        System.out.println(file.createNewFile());

        File dir = new File("dir1");
        System.out.println(dir.mkdir());

        /*
        * getAbsoluteFile: 获取文件/文件夹的绝对路径
        * getPath: 获取构造方法中传入的路径
        * getName: 获取文件/文件夹的名字
        * length: 文件获取文件内容的字节数, 文件夹未知
        * lastModified: 获取文件/文件夹的最后修改的时间戳
        * list: 获取指定目录下的所有文件/文件夹的名字, 返回值为字符串数组String[]
        *   如果文件夹下没有文件/文件夹则返回null,
        *   如果目录为文件, 则返回null
        * */
        File file1 = new File("abc");
        System.out.println("file1.getAbsoluteFile: " + file1.getAbsoluteFile());
        System.out.println("file1.getPath: " + file1.getPath());
        System.out.println("file1.getName: " + file1.getName());
        System.out.println("file1.length: " + file1.length());
        System.out.println("file1.lastModified: " + file1.lastModified());
        Date d = new Date(file.lastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        System.out.println(sdf.format(d));
        System.out.println("file1.list: " + file1.list());
        System.out.println("file1.listFiles: " + file1.listFiles());

        System.out.println("-------------------------");

        File dir1 = new File("dir1");
        System.out.println("dir1.getAbsoluteFile: " + dir1.getAbsoluteFile());
        System.out.println("dir1.getPath: " + dir1.getPath());
        System.out.println("dir1.getName: " + dir1.getName());
        System.out.println("dir1.length: " + dir1.length());
        System.out.println("dir1.lastModified: " + dir1.lastModified());
        System.out.println("dir1.list: " + dir1.list());
        String [] arr = dir1.list();
        for (String string: arr) {
            System.out.print(" " + string);
        }
        System.out.println("");
        System.out.println("dir1.listFiles: " + dir1.listFiles());
        File [] arr1 = dir1.listFiles();
        for (File temp: arr1) {
            System.out.println(" " + temp.getAbsolutePath());
        }
    }
}
